package com.central.file.controller;

import java.util.Map;

import com.central.common.annotation.RequiredPermission;
import com.central.common.constant.PermissionConstants;
import com.central.common.model.Result;
import com.central.file.service.IFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.central.common.model.PageResult;
import com.central.common.model.FileInfo;

import javax.annotation.Resource;

/**
 * 文件上传
 *
 * @author 作者 owen E-mail: 624191343@qq.com
 */
@RestController
@Api(tags = "文件模块api")
@Slf4j
public class FileController {
    @Resource
    private IFileService fileService;

    /**
     * 文件上传
     * 根据fileType选择上传方式
     *
     * @param file
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "文件上传")
    @PostMapping("/files-anon")
    public FileInfo upload(@RequestParam("file") MultipartFile file) throws Exception {
        return fileService.upload(file);
    }

    /**
     * 文件删除
     *
     * @param id
     */
    @ApiOperation(value = "文件删除")
    @DeleteMapping("/files/{id}")
    @RequiredPermission(PermissionConstants.SYS_ADMIN)
    public Result delete(@PathVariable String id) {
        try {
            fileService.delete(id);
            return Result.succeed("操作成功");
        } catch (Exception ex) {
            return Result.failed("操作失败");
        }
    }

    /**
     * 文件查询
     *
     * @param params
     * @return
     */
    @ApiOperation(value = "文件查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer")
    })
    @GetMapping("/files")
    public PageResult<FileInfo> findFiles(@RequestParam Map<String, Object> params) {
        return fileService.findList(params);
    }

    /**
     * 引用文件
     *
     * @param url
     * @param msg
     * @return
     */
    @ApiOperation(value = "引用文件")
    @GetMapping("/pv/quoteFile")
    public Long quoteFile(String url,String msg) {
        return fileService.quoteFile(url,msg);
    }

    /**
     * 删除引用
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除引用")
    @DeleteMapping("/pv/delQuote")
    public void delQuote(Long id) {
        fileService.delQuote(id);
    }

}
